import { ItemColumn } from './typing';
import { getDictTextByKey } from '/@/utils/service';
import { merge } from 'lodash-es';
import { defineComponent } from 'vue';
import { FileExplorer } from '/@/components/File';

/**
 * @param field
 * @param label
 * @param format
 */
export function textCol(
  field: string | string[],
  label: string,
  format?: (recordField) => any
): ItemColumn {
  return {
    field,
    label,
    format,
  };
}

/**
 * 列表字典
 * @param field
 * @param label
 * @param dictType
 * @param format
 */
export function dictCol(
  field: string | string[],
  label: string,
  dictType: string,
  format?: (fieldValue: any, column?: ItemColumn) => any
): ItemColumn {
  return merge(
    {
      field,
      label,
      dictType,
      format: (fieldValue: any) => {
        return getDictTextByKey(dictType, fieldValue) ?? '<未知>';
      },
    },
    { format }
  );
}

/**
 * 列表附件展示
 * @param field
 * @param label
 */
export function attachmentCol(field, label): ItemColumn {
  return {
    field,
    label,
    type: 'attachment',
    component: defineComponent({
      render: () => <FileExplorer></FileExplorer>,
    }),
  };
}
